package cn.yunhe.dao.impl;

import cn.yunhe.dao.HouseDao;
import cn.yunhe.pojo.*;
import org.springframework.jdbc.core.support.JdbcDaoSupport;


import java.util.ArrayList;
import java.util.List;

public class HouseDaoImp2JdbcTemplate extends JdbcDaoSupport implements HouseDao {

    @Override
    public House searchHouse(int hid) {
        String sql="select h.*,m.*,t.*,s.*,d.*";
        sql+="from house h ";
        sql+="inner join member m on h.hmid=m.mid ";
        sql+="inner join type t on h.htid=t.tid ";
        sql+="inner join street s on h.hsid=s.sid ";
        sql+="inner join district d on s.sdid=d.did ";
        sql+="where hid=? ";
        sql+="order by hid";
        List<House>houseList=getJdbcTemplate().query(sql,houseRowMapper,hid);
        return houseList.size()>0?houseList.get(0):null;
    }

    @Override
    public List<House> houseList() {
        String sql="select h.*,m.*,t.*,s.*,d.*";
        sql+="from house h ";
        sql+="inner join member m on h.hmid=m.mid ";
        sql+="inner join type t on h.htid=t.tid ";
        sql+="inner join street s on h.hsid=s.sid ";
        sql+="inner join district d on s.sdid=d.did ";
        sql+="order by hid";

        System.out.println(sql);
        List<House>houseList= getJdbcTemplate().query(sql,houseRowMapper);
        return houseList;
    }

    @Override
    public List<House> houseList(House house) {
        StringBuilder sql=new StringBuilder();
        sql.append("select h.*,m.*,t.*,s.*,d.*");
        sql.append("from house h ");
        sql.append("inner join member m on h.hmid=m.mid ");
        sql.append("inner join type t on h.htid=t.tid ");
        sql.append("inner join street s on h.hsid=s.sid ");
        sql.append("inner join district d on s.sdid=d.did ");
        sql.append("where 1=1  ");
        List list=new ArrayList();
        if (house!=null){
            if (house.getHtitle()!=null&&house.getHtitle().trim().length()>0){
                sql.append("and h.htitle like ? ");
                list.add("%"+house.getHtitle()+"%");
            }
            if (house.getHprice()>0){
                sql.append("and h.hprice >? ");
                list.add(house.getHprice());
            }
        }
        sql.append("order by hid ");
        System.out.println(sql);

        Object[] objects=list.toArray();
        List<House>houseList= getJdbcTemplate().query(sql.toString(),houseRowMapper,objects);
        return houseList;
    }

    @Override
    public boolean insertHouse(House house) {
        String sql="insert into house(htitle,hprice,hfloorage,hpubdate,hcontact,hdesc,hflag,hmid,htid,hsid)" ;
        sql+="values(?,?,?,?,?,?,?,?,?,?)";
        System.out.println(sql);
        Object[] objects=new Object[]
                {house.getHtitle(),house.getHprice(),house.getHfloorage(),
                        house.getHpubdate(),house.getHcontact(),house.getHdesc(),
                        house.getHflag(),house.getMember().getMid(),house.getType().getTid(),
                        house.getStreet().getSid()
                };

        int flag1=getJdbcTemplate().update(sql,objects);
        boolean flag2=false;
        if (flag1>0){
            flag2=true;
        }
        return flag2;
    }

    @Override
    public boolean updateHouse(House house) {
        return false;
    }

    @Override
    public boolean deleteHouse(House house) {
        return false;
    }
}
